enum messageEnum {
	c2sLoginMID = 1001,
	s2cLoginMID = 1101,
	c2sReadyMID = 1002,
	s2cReadyMID = 1102,
	s2cGameStartMID = 1103,
	s2cRoundEndMID = 1104,
	c2sSendEnemyMID = 1005,
	s2cSendEnemyMID = 1105,
	c2sGameoverMID = 1006,
	s2cGameoverMID = 1106,
	c2sleakEnemyMID = 1007,
	s2cleakEnemyMID = 1107,
	c2sskillMID = 1008,
	s2cskillMID = 1108,
	s2cgetGold = 1109,
	c2stoNextMID = 1010,
	s2ctoNextMID = 1110,
	c2sinviteMID = 1011,
}
class webNetworkManager {
	public isConnected:boolean;
	public constructor() {
	}
	/**心跳 */
	private hertBeatTimer: egret.Timer;
	public static instance: webNetworkManager;
	public static getInstance(): webNetworkManager {
		if (this.instance == null) {
			this.instance = new webNetworkManager();
		}
		return this.instance;
	}
	private heartTimer() {
		// this.hertBeatTimer = new egret.Timer(1000, -1);
		// this.hertBeatTimer.addEventListener(egret.TimerEvent.TIMER, e => {
		// 	if (this.isConnecteed == true)
		// 		webNetworkMananger.getInstance().SendMeesage('hert', {});
		// }, this);
		// this.hertBeatTimer.start();
	}
	//用来接受游戏场景的
	public gameSceneRec: any;
	public static test: boolean = false;
	/**服务器移动 */
	//private _root_url: string = "176.122.156.95";//bawagong
	private _root_url: string ="172.16.19.148";//wa
	//private _root_url: string = "172.16.19.190";
	//private _root_url: string = "172.16.19.104";
	public static isServerMove: boolean = false;
	public webSocket: egret.WebSocket;
	private heartMessag: egret.ByteArray;
	//CONCNET SERVER
	public connect(): void {
		if (this._root_url === null) {
			console.error("未设置连接HOST IP");
			return;
		}
		let heartMessagetmp = new awesomepackage.AwesomeMessage();
		heartMessagetmp.meesageId = 51;
		this.heartMessag = new egret.ByteArray(awesomepackage.AwesomeMessage.encode(heartMessagetmp).finish());
		// if (this.isConnecteed === true) {
		// 	console.error("服务器已经连接");
		// 	let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		// 	let m_c2sEnterRoom = new awesomepackage.c2sEnterRoom();
		// 	m_c2sEnterRoom.account = DataProl.account;
		// 	m_c2sEnterRoom.deessUp = JSON.stringify(DataProl.userCurrentDressUp);
		// 	m_AwesomeMessage.meesageId = 1002;
		// 	m_AwesomeMessage.data = awesomepackage.c2sEnterRoom.encode(m_c2sEnterRoom).finish();
		// 	var buffer =awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		// 	var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		// 	webNetworkMananger.getInstance().SendBitMeesage(byte2);
		// 	return;
		// }
		this.webSocket = new egret.WebSocket();
		//设置数据格式为二进制，默认为字符串
		this.webSocket.type = egret.WebSocket.TYPE_BINARY;
		//添加收到数据侦听，收到数据会调用此方法
		this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
		//添加链接打开侦听，连接成功会调用此方法
		this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
		//添加链接关闭侦听，手动关闭或者服务器关闭连接会调用此方法
		this.webSocket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this);
		//添加异常侦听，出现异常会调用此方法
		this.webSocket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onSocketError, this);
		//连接服务器
		this.webSocket.connect(this._root_url, 3011);
	}


	public onSocketOpen(): void {
		this.isConnected = true;
		//	LoginMain.LoginBackMess=0;
		console.log("连接成功");
	}
	public onSocketClose(): void {
		this.isConnected = false;
		console.log("连接失败");
	}
	public onSocketError(): void {
		console.log("连接报错");
	}
	public SendBitMeesage(mes: egret.ByteArray): void {
		this.webSocket.writeBytes(mes);
		this.webSocket.flush();
	}
	//recive message
	public onReceiveMessage(e: egret.Event): void {
		var byte: egret.ByteArray = new egret.ByteArray();
		this.webSocket.readBytes(byte);
		this.reciveMessageHandle(byte.bytes);
	}
	/**显示错误信息 */
	public showErrorMessage(data: any) {
		this.gameSceneRec.displayErrorMessage(data);
	}
	/**登录 */
	public LoginGameServer() {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sLogin = new awesomepackage.c2sLogin();
		m_c2sLogin.account = "Wayne";
		console.log(m_c2sLogin.account);
		m_c2sLogin.sign = "666";
		m_c2sLogin.sex = "sex";
		m_c2sLogin.name = "name";
		m_c2sLogin.userId=MsgeManage.userid;
		console.log(m_c2sLogin.userId);
		m_AwesomeMessage.meesageId = messageEnum.c2sLoginMID;
		m_AwesomeMessage.data = awesomepackage.c2sLogin.encode(m_c2sLogin).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkManager.getInstance().SendBitMeesage(byte2);
   }
   //点击开始，玩家准备状态
   public user_ready() {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sReady = new awesomepackage.c2sReady();
		m_c2sReady.userID = MsgeManage.userid;
		m_AwesomeMessage.meesageId = messageEnum.c2sReadyMID;
		m_AwesomeMessage.data = awesomepackage.c2sReady.encode(m_c2sReady).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkManager.getInstance().SendBitMeesage(byte2);
	}
   /**接受数据 */
	public reciveMessageHandle(backData:any)
	{
     var pMessaage = awesomepackage.AwesomeMessage.decode(backData);
	 console.log(pMessaage.meesageId);
	}
}